package net.oschina.victor.liu.lemon.service;

import java.util.HashSet;
import java.util.Set;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import net.oschina.victor.liu.lemon.common.MessageException;
import net.oschina.victor.liu.lemon.entity.Role;
import net.oschina.victor.liu.lemon.entity.User;
import net.oschina.victor.liu.lemon.repsotory.RoleRepsotory;
import net.oschina.victor.liu.lemon.repsotory.UserRepsotory;

@Service
public class UserServiceImpl implements UserService {
	@Autowired
	private UserRepsotory userRepsotory;
	@Autowired
	private RoleRepsotory roleRepsotory;

	@Override
	public Boolean save(User user, String[] roleIds) throws MessageException {
		Specification<User> specification = new Specification<User>() {
			@Override
			public Predicate toPredicate(Root<User> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
				Predicate p = cb.equal(root.get("username"), user.getUsername());
				return p;
			}
		};
		if (userRepsotory.findOne(specification) != null) {
			throw new MessageException("用户名称重复");
		}
		try {
			Set<Role> roles = new HashSet<Role>();
			for (int i = 0; i < roleIds.length; i++) {
				roles.add(roleRepsotory.findOne(Long.getLong(roleIds[i])));
			}
			user.setRoles(roles);// 将选中的角色列表设置给用户
			userRepsotory.save(user);
		} catch (Exception e) {
			e.printStackTrace();
			throw new MessageException("用户信息保存失败");
		}
		return true;

	}

	@Override
	public Boolean update(User user, String[] roleIds) throws MessageException {
		try {
			Set<Role> roles = new HashSet<Role>();
			for (int i = 0; i < roleIds.length; i++) {
				roles.add(roleRepsotory.findOne(Long.parseLong(roleIds[i])));
			}
			user.setRoles(roles);// 将选中的角色列表设置给用户
			userRepsotory.save(user);
		} catch (Exception e) {
			e.printStackTrace();
			throw new MessageException("用户信息保存失败");
		}
		return true;
	}

	@Override
	public Boolean delete(Long id) throws MessageException {
		// TODO Auto-generated method stub
		return null;
	}

	@Transactional(propagation = Propagation.NOT_SUPPORTED, readOnly = true)
	public Page<User> list(String keyword, Integer pageNo, Integer pageSize) {
		Specification<User> specification = new Specification<User>() {
			@Override
			public Predicate toPredicate(Root<User> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
				Predicate predicate = cb.or(cb.like(root.get("username"), "%" + keyword + "%"),
						cb.like(root.get("nickname"), "%" + keyword + "%"));
				// predicate = cb.and(predicate, cb.equal(root.get("available"),
				// available));
				return predicate;
			}
		};
		return userRepsotory.findAll(specification, new PageRequest(pageNo, pageSize));
	}

	@Override
	@Transactional(propagation = Propagation.NOT_SUPPORTED, readOnly = true)
	public User getUser(Long id) {
		return userRepsotory.findOne(id);
	}

	@Override
	public User getUser(String username) {
		Specification<User> specification = new Specification<User>() {
			@Override
			public Predicate toPredicate(Root<User> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
				Predicate predicate = cb.like(root.get("username"), username);
				return predicate;
			}
		};
		return userRepsotory.findOne(specification);
	}

	@Override
	public Set<String> findRoles(String username) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public Set<String> findPermissions(String username) {
		// TODO Auto-generated method stub
		return null;
	}

}
